//打开的是app,还是其他的浏览器. 如果不是app,弹出下载dialog
//ccjk是在webview里面设置的
function isFromApp() {
    return navigator.userAgent === "ccjk";

}


function unauthorizedApiError(error) {
    if (error.status == 401) toast("need login");
}


function stringToJsonString(originString) {
    return JSON.parse(originString.replace(/&quot;/g, '"'))
}

//如果是在网页打开的,点击下载按钮执行的方法
function onDownloadDialogButtonClick() {
    //需要
    window.location.href = "https://codeload.github.com/FezVrasta/popper.js/zip/v1.14.1"
    $("#download-dialog").modal('hide')
}


function jsCallAndroid(event) {
    return window.prompt(event, null)
}

function toast(message) {
    var html = '<div id="app-toast" class="pl-2 pr-2 pt-1 pb-1" ' +
        'style="z-index:10;text-align:center;min-width:10rem;border: 1px solid #ddd;' +
        'background: #000000;color:#FFFFFF;position:absolute;left:50%;top:50%;transform:translateX(-50%);' +
        '-ms-transform:translateX(-50%); -moz-transform:translateX(-50%); ' +
        '-webkit-transform:translateX(-50%);-o-transform:translateX(-50%); ">'
        + message +
        '</div>'
    $("#app-toast").remove()
    $("body").append(html)
    setTimeout("$('#app-toast').remove()", 3000)

    // if( navigator.userAgent === "ccjk"){
    //
    //
    //     window.alert(message)
    // }else {
    //     window.document.
    //     window.alert(message)
    // }

}


//显示刚刚(10分钟内),5分钟前,5小时前,5天前
//date is long
function dateToString(date) {
    var diff = new Date().getTime() - date
    if (diff < 10 * 60 * 1000) {//小于10分钟
        return "刚刚"
    } else if (diff < 60 * 60 * 1000) {//小于1个小时
        return parseInt(diff / (60 * 1000)) + "分钟前"
    } else if (diff < 24 * 60 * 60 * 1000) {//小于1天
        return parseInt(diff / (60 * 60 * 1000)) + "小时前"
    } else {
        return parseInt(diff / (24 * 60 * 60 * 1000)) + "天前"
    }

}

//
// function createLoginDialog(){
//     return `
//     <div class="modal fade " id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
//       <div class="modal-dialog modal-dialog-centered" role="document">
//         <div class="modal-content ">
//           <div class="modal-header">
//             <h5 class="modal-title h4 text-primary font-weight-bold" id="loginModaltitle">需要登录</h5>
//             <button type="button" class="close" data-dismiss="modal" aria-label="Close">
//               <span aria-hidden="true">&times;</span>
//             </button>
//           </div>
//           <div class="modal-body">
//             您的会话已过期。 请再次登录
//           </div>
//           <div class="modal-footer">
//             <button type="button" class="btn btn-lg btn-primary" onclick="function(){window.location.href='/html/user/login'}">
//                 <i class="fa fa-user-circle "></i>
//                 去登陆
//             </button>
//           </div>
//         </div>
//       </div>
//     </div>
//     `
// }

function createLoginDialog() {
    return `
<div class="modal fade " id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
   aria-hidden="true">
   <a class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content ">
         <div class="modal-header">
            <h5 class="modal-title h4 text-primary font-weight-bold" id="loginModaltitle">需要登录</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
         </div>
         <div class="modal-body">
            <div style="" class="container">
               <form>
                  <div class="text-center">
                     <div class="row">
                        <div class="col-sm-12 col-md-10 col-md-offset-1">
                           <p id="error" class="float-left text-danger font-weight-bold hide">用户密码错误</p>
                        </div>
                     </div>
                     <div class="row">
                        <div class="col-sm-12 col-md-10 col-md-offset-1">
                           <div class="form-group">
                              <div class="input-group input-group-lg">
                                 <input id="emailInput" placeholder="邮件" name="email" type="text"
                                    autofocus="" class="form-control"/>
                              </div>
                           </div>
                           <div class="form-group">
                              <div class="input-group input-group-lg">
                                 <input id="passInput" placeholder="密码" name="password" type="password"
                                    value="" class="form-control"/>
                              </div>
                           </div>
                        </div>
                     </div>
                     <button type="button" class="btn btn-block btn-lg btn-primary"
                        onclick="loginModal()">
                        <i class="fa fa-user-circle "></i>
                        去登陆
                     </button>
               </form>
               </div>
            </div>
         </div>
         <div class="modal-footer">
            
          
            
            
            <span class="text-danger" onclick="window.location='/html/user/register'">立即注册</span>
            
            <span class="pl-5 pr-3">忘记密码？</span>
         </div>
      </div>
   </div>
    
    <script>
       
       
    </script>
</div>    `
}

function loginModal() {
    var errorLabel = $("#error");
    var showError = function (msg) {
        errorLabel.text(msg)
        errorLabel.show();
    };
    var hideError = function () {
        errorLabel.hide();
    };

    function validateEmail(email) {
        var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(String(email).toLowerCase());
    }

    hideError();
    var email = $("#emailInput").val()
    var password = $("#passInput").val()

    if (email.length < 4 || !validateEmail(email)){
        showError("需要有效的电邮地址");
        return;
    }else{
        if (password.length < 6){
            showError("密码至少需要5个字符")
            return;
        }
    }
    $.ajax({
        type: "POST",

        url: "/user/loginUsingEmail",
        data: "email=" + email + "&password=" + password,

        success: function (result) {
            console.log(result)
            window.location.href="/html/user/home"
        },
        error: function (res) {
            console.log(res)
            if (res.responseJSON!=null){
                var resp = res.responseJSON
                if (resp.code==4321){
                    showError("用户密码错误");

                }
            }
        }

    });
}


function createDownloadDialog() {
    return `
    <div class="modal fade" id="download-dialog" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
         aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header"><h5 class="modal-title" id="exampleModalLongTitle">网页中无法使用改功能</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                </div>
                <div class="modal-body">前去下载app</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">取消</button>
                    <button class="btn btn-primary" type="button" onClick="onDownloadDialogButtonClick()">下载</button>
                </div>
            </div>
        </div>
    </div>
</div>
`
}